import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:async';


class MyTestUpload extends StatefulWidget {
  MyTestUpload({Key key}) : super(key: key);

  _MyTestUploadState createState() => _MyTestUploadState();
}
//
class _MyTestUploadState extends State<MyTestUpload> {
  //记录选择的照片
  File _image;
  var imagePath;

  //当图片上传成功后，记录当前上传的图片在服务器中的位置
  String _imgServerPath;

  //拍照
  Future _getImageFromCamera() async {
    var image =  await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400);
    setState(() {
      _image = image;
    });
  }

  //相册选择
  Future _getImageFromGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    _uploadImage(image);
    setState(() {
      _image = image;
    });
  }



  _uploadImage(image) async {
    String path = image.path;
    print(path);
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    print(name);
    var suffix = name.substring(name.lastIndexOf(".") + 1, name.length);
    print(suffix);


    var formData = FormData.fromMap({
      "userId": "10000024",
      "file": await MultipartFile.fromFile(path, filename: name)
    });

    var response =
    await Dio().post("http://jd.itying.com/imgupload", data: formData);
    print(response);
    if (response.statusCode == 200) {
      Map responseMap = response.data;
      print("http://jd.itying.com${responseMap["path"]}");
      setState(() {
        _imgServerPath = "http://jd.itying.com${responseMap["path"]}";
      });
    }
  }
  





  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("选择图片并上传")),
      body: Container(
        child: ListView(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                _getImageFromCamera();
              },
              child: Text("照相机"),
            ),
            SizedBox(height: 10),
            RaisedButton(
              onPressed: () {
                _getImageFromGallery();
              },
              child: Text("相册"),
            ),
            SizedBox(height: 10),
            /**
             * 展示选择的图片
             */
            _image == null
                ? Text("no image selected")
                : Image.file(
              _image,
              fit: BoxFit.cover,
            ),
            SizedBox(height: 10),
            RaisedButton(
              onPressed: () {
//                _uploadImage(image);
              },
              child: Text("上传图片到服务器"),
            ),
            SizedBox(height: 10),
            _imgServerPath == null
                ? Text("没有上传图片")
                : Image.network(
                "http://jd.itying.com/public/upload/daoyXVTvrCCUeoIliZtNXX-s.png"),
          ],
        ),
      ),
    );
  }
}













//import 'package:image_picker/image_picker.dart';
//import 'package:flutter/material.dart';
//
//class MyTestUpload extends StatefulWidget {
//  @override
//  _MyTestUploadState createState() => _MyTestUploadState();
//}
//
//class _MyTestUploadState extends State<MyTestUpload> {
//  var _image;
//
//  Future getImage() async {
//    var image = await ImagePicker.pickImage(source: ImageSource.camera);
//    print('image--->>>>:${image}');
//    setState(() {
//      _image = image;
//    });
//  }
//
//  @override
//  Widget build(BuildContext context) {
//    return Scaffold(
//      appBar: AppBar(
//        title: Text('Image Picker Example'),
//      ),
//      body: Center(
//        child: _image == null
//            ? Text('No image selected.')
//            : Image.file(_image),
//      ),
//      floatingActionButton: FloatingActionButton(
//        onPressed: getImage,
//        tooltip: 'Pick Image',
//        child: Icon(Icons.add_a_photo),
//      ),
//    );
//  }
//}

